<?php

/**
 * Albireo Kernel
 *
 * @copyright  Copyright (c) 2010 Albireo Solutions
 * @package    Kernel
 */

namespace KernelModule\ModulesModule;

use Albireo\Forms\AlbireoForm;

use KernelModule\BasePresenter;

use Celebrio\FileSystem\VirtualFileSystem;
use Celebrio\FileSystem\VirtualFile;
use Celebrio\FileSystem\DataFile;
use Celebrio\FileSystem\Folder;
use Celebrio\Core\ServiceProvider;
use Celebrio\Modules;

/**
 * Kernel modules management presenter.
 *
 * @author     Albireo Solutions
 * @package    Kernel
 */
class TerminalPresenter extends BasePresenter {

    public function actionDefault() {
        $namespace = $this->getSession("terminal");
        if (isset($namespace->queries)) {
            $this->template->queries = $namespace->queries;
        }
    }

    
    public function renderDefault() {
        $this->showMenu();
        $this->template->title = _("Filesystem terminal");
        $this->template->form = $this['console'];

        
        //$this->testingSaveRoot();
        //$this->testingLoadRoot();
        //$this->testingSave();
        //$this->testingLoad();
        //$this->testingMove();
        //$this->testingDelete();
        //$this->testingDeepDelete();
        //$this->testingFileInfo();
        //$this->testingCopy();
    }

    private function testingSaveRoot() {
        $file = new DataFile("podslozka21", "/slozka", DataFile::DIR);
        $fs = new VirtualFileSystem($this->getUser());
        $fs->save($file);
    }
    
    private function testingLoadRoot() {
        $file = new DataFile("podslozka21", "/slozka", DataFile::DIR);
        $fs = new VirtualFileSystem($this->getUser());
        $name = $fs->load($file);
        $info = $fs->getFileInfo($file);
        
    }
    
    private function testingCopy() {
        $from = new DataFile("albroot", "/home/");
        $to = new DataFile("newalbroot", "/newhome/");
        $fs = new VirtualFileSystem($this->getUser());
        $fs->copy($from, $to, true);
    }

    private function testingFileInfo() {
        $file = new DataFile("albroot", "/home");
        $fs = new VirtualFileSystem($this->getUser());
        $loaded = $fs->getFileInfo($file);
        //\Nette\Debug::dump($loaded);
    }

    private function testingDeepDelete() {
        $file = new DataFile("toDelete", "/podadresar/", "text/plain");
        $fs = new VirtualFileSystem($this->getUser());
        $fs->save($file);
        $toDelete = new DataFile("podadresar", "/", DataFile::DIR);
        $fs->delete($toDelete);
    }
    private function testingDelete() {
        $file = new DataFile("home", "/");
        $fs = new VirtualFileSystem($this->getUser());
        $loaded = $fs->delete($file);
    }

    private function testingMove() {
        //$from = new DataFile("albroot", "/home/");
        $to = new DataFile("novyhome", "/nekde/jinde/nevim/vubec/kde");
        $from = new DataFile("home", "/");
        $fs = new VirtualFileSystem($this->getUser());
        $fs->move($from, $to);
    }

    private function testingSave() {
        $file = new DataFile("c.xml", "/home/albroot", "text/plain");
        $file->setData("<root>\nahooooooooooj\n</root>");
        $fs = new VirtualFileSystem($this->getUser());
        $fs->save($file);
    }

    private function testingLoad() {
        $file = new DataFile("slozka", "/");
        $fs = new VirtualFileSystem($this->getUser());
        $loaded = $fs->load($file);
        \Nette\Debug::dump($loaded);
        \Nette\Debug::dump("Entry (file or directory) mimetype: " . $loaded->getMimetypeText());
        if ($loaded->isDirectory()) {
            \Nette\Debug::dump("Directory content:");
            foreach ($loaded->getData() as $entry) {
                \Nette\Debug::dump($entry->getUrl() . $entry->getName());
            }
        } else {
            \Nette\Debug::dump("File content: \n" . $loaded->getData());
        }
    }

    protected function createComponentConsole() {
        $form = new AlbireoForm();
        $form->addTextArea("albireoConsole", "[FS /]" . "$", NULL, 1);
        $form->addSubmit('process', _('Process'));
        $form->onSubmit[] = callback($this, 'processQuery');
        return $form;
    }

    public function processQuery($form) {
        $values = $form->values;
        $this->callConsole($values['albireoConsole']);
    }

    private function callConsole($query) {
        $namespace = $this->getSession("terminal");
        if (isset($namespace->queries)) {
            $namespace->queries[] = $query;
        } else {
            $namespace->queries = array($query);
        }
        try {
            $model = new FSConsoleModel();
            $newQuery = $model->parseQuery($query);
        } catch(\Exception $e) {
            $query = $e->getMessage();
        }
        throw new \Exception($query);
        $this->template->queries = $namespace->queries;
        $this->template->console = $query;
    }

    public function handleProcess($query) {
        $query = htmlspecialchars($query);
        $this->invalidateControl("console");
        $this->invalidateControl("list"); // should this be here?
        $this->callConsole($query);
    }
}
